﻿using System;
using LRS.DTO;
using System.Collections.Generic;
using System.Data.SqlClient;
namespace LRS.DAL
{
    public static class dSupplier
    {
        public static List<eSupplier> GetSuppliers()
        {
            List<eSupplier> listSupplier = new List<eSupplier>();
            SqlDataReader reader = SqlHelper.ExecuteReader("Select * from Supplier Order by SupplierName ASC");
            while (reader.Read())
            {
                eSupplier supplier = new eSupplier();
                supplier.Address = reader["Address"].ToString();
                supplier.Email = reader["Email"].ToString();
                supplier.SupplierID = Int32.Parse(reader["SupplierID"].ToString());
                supplier.SupplierName = reader["SupplierName"].ToString();
                supplier.SupplierNumber = reader["SupplierNumber"].ToString();
                supplier.Phone = reader["Phone"].ToString();
                supplier.OtherInfo = reader["OtherInfo"].ToString();
                listSupplier.Add(supplier);
            }
            reader.Close();
            return listSupplier;
        }
        public static eSupplier GetSupplierByID(int SupplierID)
        {
            SqlDataReader reader = SqlHelper.ExecuteReader("Select * from Supplier where SupplierID=" + SupplierID.ToString());
            if (!reader.HasRows)
                return null;
            reader.Read();
            eSupplier supplier = new eSupplier();
            supplier.Address = reader["Address"].ToString();
            supplier.Email = reader["Email"].ToString();
            supplier.SupplierID = Int32.Parse(reader["SupplierID"].ToString());
            supplier.SupplierName = reader["SupplierName"].ToString();
            supplier.SupplierNumber = reader["SupplierNumber"].ToString();
            supplier.Phone = reader["Phone"].ToString();
            supplier.OtherInfo = reader["OtherInfo"].ToString();

            reader.Close();
            return supplier;
        }
        public static List<eSupplier> GetSuppliersByName(string name)
        {
            List<eSupplier> listSupplier = new List<eSupplier>();
            SqlDataReader reader = SqlHelper.ExecuteReader("Select * from Supplier where SupplierName like '" + name + "'");
            while (reader.Read())
            {
                eSupplier supplier = new eSupplier();
                supplier.Address = reader["Address"].ToString();
                supplier.Email = reader["Email"].ToString();
                supplier.SupplierID = Int32.Parse(reader["SupplierID"].ToString());
                supplier.SupplierName = reader["SupplierName"].ToString();
                supplier.SupplierNumber = reader["SupplierNumber"].ToString();
                supplier.Phone = reader["Phone"].ToString();
                supplier.OtherInfo = reader["OtherInfo"].ToString();
                listSupplier.Add(supplier);
            }
            reader.Close();
            return listSupplier;
        }
        public static void InsertSupplier(eSupplier supplier)
        {
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("@SupplierName", supplier.SupplierName));
            parameters.Add(new SqlParameter("@Address", supplier.Address));
            parameters.Add(new SqlParameter("@Phone", supplier.Phone));
            parameters.Add(new SqlParameter("@Email", supplier.Email));
            parameters.Add(new SqlParameter("@OtherInfo", supplier.OtherInfo));
            SqlHelper.ExecuteNonQuery("Insert into Supplier values(@SupplierName, @Address, @Email, @Phone, @OtherInfo)", parameters);
        }
        public static void UpdateSupplier(eSupplier supplier)
        {
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("@SupplierName", supplier.SupplierName));
            parameters.Add(new SqlParameter("@Address", supplier.Address));
            parameters.Add(new SqlParameter("@Phone", supplier.Phone));
            parameters.Add(new SqlParameter("@Email", supplier.Email));
            parameters.Add(new SqlParameter("@OtherInfo", supplier.OtherInfo));
            parameters.Add(new SqlParameter("@SupplierID", supplier.SupplierID));
            SqlHelper.ExecuteNonQuery("Update Supplier set SupplierName=@SupplierName, Address=@Address, Phone=@Phone, Email=@Email, OtherInfo=@OtherInfo where SupplierID=@SupplierID", parameters);
        }
        public static void DeleteSupplierByID(int SupplierID)
        {
            SqlHelper.ExecuteNonQuery("Delete from Supplier where SupplierID=" + SupplierID);
        }
    }
}
